package com.samtech.web.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.client.ClientFactory;
import com.open.client.OpenApiClient;
import com.open.client.base.JsonUtil;
import com.open.client.req.TradeAliJsApiRequest;
import com.open.client.req.TradeAliNativeRequest;
import com.open.client.req.TradeQQMaRequest;
import com.open.client.req.YinlianConsumeRequest;
import com.open.client.resp.TradeAliJsApiResponse;
import com.open.client.resp.TradeOrderConsumeResponse;

@Controller
public class TestHHPayController {
	static SimpleDateFormat ymd=new SimpleDateFormat("yyyyMMdd");
	static String strymd=ymd.format(new Date());
	@RequestMapping(value="/testpage",method=RequestMethod.GET)
	public String getIndex() {
		System.out.println("get index page");
		return "index_th";
	}
	static String merchant_num="ba21f5349d2c9d7d2ce0a94763fd46d9";
	AtomicInteger nxtid=new AtomicInteger(1);
	@RequestMapping(value="/testpage",method=RequestMethod.POST)
	@ResponseBody
	public String doApiInvoke(HttpServletRequest httpReq) {
		String orderid = httpReq.getParameter("orderid");
		String goodname = httpReq.getParameter("goodname");
		String money = httpReq.getParameter("money");
		System.out.println("get index page");
		OpenApiClient client = ClientFactory.getInstance().buildOrderClient();
		TradeAliNativeRequest request = new TradeAliNativeRequest();
		request.setClientIp("127.0.0.1");
		request.setRequestId(strymd+ThreadLocalRandom.current().nextInt(7) +ThreadLocalRandom.current().nextInt(5)+"2431000"+nxtid.getAndIncrement());
		request.setPartnerNo("s628EINc3e");
		request.setVersion("1.0.0");
		request.setMerchantNum(merchant_num);
		request.setAisleNo("C5");//C2
		request.setProvince("5800");
		request.setCity("5810");
		request.setArea("5810");
		request.setOrgOrderNo(orderid);
		request.setPayAmount(Integer.valueOf(money));
		request.setNotifyUrl("http://www.fjtdmall.com/index.htm");
		request.setProductName(goodname);
		try {
			TradeOrderConsumeResponse aliNativePay = client.aliNativePay(request);
			if(aliNativePay!=null) {
				String x = JsonUtil.obj2Jsonstr(aliNativePay);
				System.out.println("alipay result:"+x);
				String content = aliNativePay.getContent();
				System.out.println("alipay content:"+content);
				if(content.indexOf("</html>")>0) {
					final String stronload = "onload=";
					int ix = content.indexOf(stronload);
					if(ix>0) {
						int  sx=0;
						int ex=0;
						String sq=null;
						for(int jj=ix+stronload.length();jj<ix+50;jj++) {
							String str = content.substring(jj, jj+1);
							if(sx<=0) {
								if(str.equals("\"")) {
									sq=str;
									sx=jj+1;
								}else if(str.equals("'")) {
									sq=str;
									sx=jj+1;
								}else if(str !=null && !str.equals("=") && str.equals(" ")) {
									sx=jj;
								}
							}else if(sx>0 && ((sq!=null && sq.equals(str))||(sq==null && str.equals(">")))) {
								ex=jj;
								break;
							}
						}
						if(sx>0 && ex>0) {
							String xscript=content.substring(sx, ex);
							int bend = content.indexOf("</html>");
							StringBuffer buf=new StringBuffer();
							buf.append(content.substring(0, bend));
							buf.append("<script type='text/javascript'>").append(xscript).append(";").append("</script>");
							buf.append(content.substring(bend));
							content=buf.toString();
						}
					}
				}
				return content;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping(value="/testpageh5",method=RequestMethod.POST)
	@ResponseBody
	public String doApiInvokeH5(HttpServletRequest httpReq) {
		String orderid = httpReq.getParameter("orderid");
		String goodname = httpReq.getParameter("goodname");
		String money = httpReq.getParameter("money");
		System.out.println("get index page");
		OpenApiClient client = ClientFactory.getInstance().buildOrderClient();
		TradeAliJsApiRequest request = new TradeAliJsApiRequest();
		request.setClientIp("127.0.0.1");
		request.setRequestId(strymd+ThreadLocalRandom.current().nextInt(9) +ThreadLocalRandom.current().nextInt(7)+"1462000"+nxtid.getAndIncrement());
		request.setPartnerNo("s628EINc3e");
		request.setVersion("1.0.0");
		request.setMerchantNum(merchant_num);
		request.setAisleNo("D2");//C2
		/*request.setProvince("5800");
		request.setCity("5810");
		request.setArea("5810");*/
		request.setOrgOrderNo(orderid);
		request.setPayAmount(Integer.valueOf(money));
		request.setNotifyUrl("http://www.fjtdmall.com/index.htm");
		request.setProductName(goodname);
		try {
			TradeAliJsApiResponse aliNativePay = client.aliJsApiPay(request);
			if(aliNativePay!=null) {
				String x = JsonUtil.obj2Jsonstr(aliNativePay);
				System.out.println("alipay result:"+x);
				String content = aliNativePay.getPayInfo();
				System.out.println("alipay content:"+content);
				if(content.indexOf("</html>")>0) {
					final String stronload = " onload=";
					int ix = content.indexOf(stronload);
					if(ix>0) {
						int  sx=0;
						int ex=0;
						String sq=null;
						for(int jj=ix+stronload.length();jj<ix+50;jj++) {
							String str = content.substring(jj, jj+1);
							if(sx<=0) {
								if(str.equals("\"")) {
									sq=str;
									sx=jj+1;
								}else if(str.equals("'")) {
									sq=str;
									sx=jj+1;
								}else if(str !=null && !str.equals("=") && str.equals(" ")) {
									sx=jj;
								}
							}else if(sx>0 && ((sq!=null && sq.equals(str))||(sq==null && str.equals(">")))) {
								ex=jj;
								break;
							}
						}
						if(sx>0 && ex>0) {
							String xscript=content.substring(sx, ex);
							int bend = content.indexOf("</html>");
							StringBuffer buf=new StringBuffer();
							buf.append(content.substring(0, bend));
							buf.append("<script type='text/javascript'>").append(xscript).append(";").append("</script>");
							buf.append(content.substring(bend));
							content=buf.toString();
						}
					}
				}
				return content;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value="/testpage1",method=RequestMethod.POST)
	@ResponseBody
	public String doApiInvoke1(HttpServletRequest httpReq) {
		String orderid = httpReq.getParameter("orderid");
		String goodname = httpReq.getParameter("goodname");
		String money = httpReq.getParameter("money");
		System.out.println("get index page");
		OpenApiClient client = ClientFactory.getInstance().buildOrderClient();
		TradeQQMaRequest request = new TradeQQMaRequest();
		request.setClientIp("127.0.0.1");
		request.setRequestId(strymd+ThreadLocalRandom.current().nextInt(7) +ThreadLocalRandom.current().nextInt(6)+"1631000"+nxtid.getAndIncrement());
		request.setPartnerNo("s628EINc3e");
		request.setVersion("1.0.0");
		request.setMerchantNum(merchant_num);
		request.setAisleNo("C4");//C2
		/*request.setProvince("5800");
		request.setCity("5810");
		request.setArea("5810");*/
		request.setOrgOrderNo(orderid);
		request.setPayAmount(Integer.valueOf(money));
		//request.setNotifyUrl("http://www.fjtdmall.com/index.htm");
		request.setProductName(goodname);
		try {
			TradeOrderConsumeResponse aliNativePay = client.qqMaPay(request);
			if(aliNativePay!=null) {
				String x = JsonUtil.obj2Jsonstr(aliNativePay);
				System.out.println("qqma result:"+x);
				String content = aliNativePay.getContent();
				System.out.println("qqma content:"+content);
				if(content.indexOf("</html>")>0) {
					final String stronload = "onload=";
					int ix = content.indexOf(stronload);
					if(ix>0) {
						int  sx=0;
						int ex=0;
						String sq=null;
						for(int jj=ix+stronload.length();jj<ix+50;jj++) {
							String str = content.substring(jj, jj+1);
							if(sx<=0) {
								if(str.equals("\"")) {
									sq=str;
									sx=jj+1;
								}else if(str.equals("'")) {
									sq=str;
									sx=jj+1;
								}else if(str !=null && !str.equals("=") && str.equals(" ")) {
									sx=jj;
								}
							}else if(sx>0 && ((sq!=null && sq.equals(str))||(sq==null && str.equals(">")))) {
								ex=jj;
								break;
							}
						}
						if(sx>0 && ex>0) {
							String xscript=content.substring(sx, ex);
							int bend = content.indexOf("</html>");
							StringBuffer buf=new StringBuffer();
							buf.append(content.substring(0, bend));
							buf.append("<script type='text/javascript'>").append(xscript).append(";").append("</script>");
							buf.append(content.substring(bend));
							content=buf.toString();
						}
					}
				}
				return content;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value="/testpagewxh5",method=RequestMethod.POST)
	@ResponseBody
	public String doApiInvokewxh5(HttpServletRequest httpReq) {
		String orderid = httpReq.getParameter("orderid");
		String goodname = httpReq.getParameter("goodname");
		String money = httpReq.getParameter("money");
		System.out.println("get index page");
		OpenApiClient client = ClientFactory.getInstance().buildOrderClient();
		TradeQQMaRequest request = new TradeQQMaRequest();
		request.setClientIp("127.0.0.1");
		request.setRequestId(strymd+ThreadLocalRandom.current().nextInt(9) +ThreadLocalRandom.current().nextInt(5)+"1436000"+nxtid.getAndIncrement());
		request.setPartnerNo("s628EINc3e");
		request.setVersion("1.0.0");
		request.setMerchantNum(merchant_num);
		request.setAisleNo("C4");//C2
		/*request.setProvince("5800");
		request.setCity("5810");
		request.setArea("5810");*/
		request.setOrgOrderNo(orderid);
		request.setPayAmount(Integer.valueOf(money));
		//request.setNotifyUrl("http://www.fjtdmall.com/index.htm");
		request.setProductName(goodname);
		try {
			TradeOrderConsumeResponse aliNativePay = client.wxH5Pay(request);
			if(aliNativePay!=null) {
				String x = JsonUtil.obj2Jsonstr(aliNativePay);
				System.out.println("qqma result:"+x);
				String content = aliNativePay.getContent();
				System.out.println("qqma content:"+content);
				if(content.indexOf("</html>")>0) {
					final String stronload = "onload=";
					int ix = content.indexOf(stronload);
					if(ix>0) {
						int  sx=0;
						int ex=0;
						String sq=null;
						for(int jj=ix+stronload.length();jj<ix+50;jj++) {
							String str = content.substring(jj, jj+1);
							if(sx<=0) {
								if(str.equals("\"")) {
									sq=str;
									sx=jj+1;
								}else if(str.equals("'")) {
									sq=str;
									sx=jj+1;
								}else if(str !=null && !str.equals("=") && str.equals(" ")) {
									sx=jj;
								}
							}else if(sx>0 && ((sq!=null && sq.equals(str))||(sq==null && str.equals(">")))) {
								ex=jj;
								break;
							}
						}
						if(sx>0 && ex>0) {
							String xscript=content.substring(sx, ex);
							int bend = content.indexOf("</html>");
							StringBuffer buf=new StringBuffer();
							buf.append(content.substring(0, bend));
							buf.append("<script type='text/javascript'>").append(xscript).append(";").append("</script>");
							buf.append(content.substring(bend));
							content=buf.toString();
						}
					}
				}
				return content;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value="/testgwpage",method=RequestMethod.POST)
	@ResponseBody
	public String dogatewayApiInvoke(HttpServletRequest httpReq) {
		String orderid = httpReq.getParameter("orderid");
		String goodname = httpReq.getParameter("goodname");
		String money = httpReq.getParameter("money");
		System.out.println("get index page");
		OpenApiClient client = ClientFactory.getInstance().buildOrderClient();
		YinlianConsumeRequest request = new YinlianConsumeRequest();
		request.setClientIp("127.0.0.1");
		request.setRequestId(strymd+ThreadLocalRandom.current().nextInt(7) +ThreadLocalRandom.current().nextInt(5)+"1431000"+nxtid.getAndIncrement());
		request.setPartnerNo("s628EINc3e");
		request.setVersion("1.0.0");
		request.setMerchantNum(merchant_num);
		request.setAisleNo("C6");//C2
		//request.setProvince("5800");
		//request.setCity("5810");
		//request.setArea("5810");
		request.setOrgOrderNo(orderid);
		request.setFrontNotifyUrl("http://www.fjtdmall.com/index.htm");
		request.setBackNotifyUrl("http://www.fjtdmall.com/index.htm");
		
		request.setPayAmount(new BigDecimal(money));
		//request.setNotifyUrl("http://www.fjtdmall.com/index.htm");
		request.setProductName(goodname);
		try {
			TradeOrderConsumeResponse aliNativePay = client.ylgwPay(request);
			if(aliNativePay!=null) {
				String x = JsonUtil.obj2Jsonstr(aliNativePay);
				System.out.println("yingliangatewaypay result:"+x);
				String content = aliNativePay.getContent();
				System.out.println("yingliangatewaypay content:"+content);
				if(content.indexOf("</html>")>0) {
					final String stronload = "onload=";
					int ix = content.indexOf(stronload);
					if(ix>0) {
						int  sx=0;
						int ex=0;
						String sq=null;
						for(int jj=ix+stronload.length();jj<ix+50;jj++) {
							String str = content.substring(jj, jj+1);
							if(sx<=0) {
								if(str.equals("\"")) {
									sq=str;
									sx=jj+1;
								}else if(str.equals("'")) {
									sq=str;
									sx=jj+1;
								}else if(str !=null && !str.equals("=") && str.equals(" ")) {
									sx=jj;
								}
							}else if(sx>0 && ((sq!=null && sq.equals(str))||(sq==null && str.equals(">")))) {
								ex=jj;
								break;
							}
						}
						if(sx>0 && ex>0) {
							String xscript=content.substring(sx, ex);
							int bend = content.indexOf("</html>");
							StringBuffer buf=new StringBuffer();
							buf.append(content.substring(0, bend));
							buf.append("<script type='text/javascript'>").append(xscript).append(";").append("</script>");
							buf.append(content.substring(bend));
							content=buf.toString();
						}
					}
				}
				return content;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
}
